package com.aquarium.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.aquarium.R;
import com.aquarium.mail.MailInfo;
import com.aquarium.mail.MailSender;

public class MailSettingActivity extends Activity implements OnClickListener {

	MailInfo mailInfo;
	MailSender mailSender;
	boolean isSSL;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mail_seeting);
		mailInfo = MailInfo.getInstance(this);
		mailSender = new MailSender(this);
		initView();
	}

	private void initView() {
		findViewById(R.id.ok).setOnClickListener(this);
		findViewById(R.id.cancel).setOnClickListener(this);
		findViewById(R.id.send_mail).setOnClickListener(this);
		((EditText) findViewById(R.id.to_address)).setText(mailInfo
				.getToAddress());
		((EditText) findViewById(R.id.username))
				.setText(mailInfo.getUserName());
		((EditText) findViewById(R.id.password))
				.setText(mailInfo.getPassword());
		((EditText) findViewById(R.id.mail_server)).setText(mailInfo
				.getMailServerHost());
		((EditText) findViewById(R.id.mail_port)).setText(mailInfo
				.getMailServerPort());
		Spinner spinner = (Spinner) findViewById(R.id.select_isssl);
		spinner.setAdapter(ArrayAdapter.createFromResource(this,
				R.array.select_ssl_array, android.R.layout.simple_spinner_item));
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				switch (arg2) {
				case 0:
					isSSL = false;
					break;
				case 1:
					isSSL = true;
					break;
				default:
					break;
				}
				showPortView(isSSL);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		if (mailInfo.getIsSSL().equals("true")) {
			isSSL = true;
			spinner.setSelection(1);
		} else {
			isSSL = false;
			spinner.setSelection(0);
		}
		showPortView(isSSL);
	}

	private void saveAllSetting() {
		Log.d("", "saveAllSetting"
				+ ((EditText) findViewById(R.id.username)).getText().toString()
						.trim());
		mailInfo.setToAddress(((EditText) findViewById(R.id.to_address))
				.getText().toString().trim());
		mailInfo.setUserName(((EditText) findViewById(R.id.username)).getText()
				.toString().trim());
		mailInfo.setPassword(((EditText) findViewById(R.id.password)).getText()
				.toString().trim());
		mailInfo.setMailServerHost(((EditText) findViewById(R.id.mail_server))
				.getText().toString().trim());
		mailInfo.setMailServerPort(((EditText) findViewById(R.id.mail_port))
				.getText().toString().trim());
		if (isSSL) {
			mailInfo.setIsSSL("true");
		} else {
			mailInfo.setIsSSL("false");
		}

	}

	void showPortView(boolean value) {
		if (value) {
			findViewById(R.id.textView5).setVisibility(View.VISIBLE);
			findViewById(R.id.mail_port).setVisibility(View.VISIBLE);
		} else {
			findViewById(R.id.textView5).setVisibility(View.GONE);
			findViewById(R.id.mail_port).setVisibility(View.GONE);
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ok:
			saveAllSetting();
			this.finish();
			break;
		case R.id.cancel:

			this.finish();
			break;
		case R.id.send_mail:
			new Thread(new Runnable() {
				@Override
				public void run() {
					mailSender.sendTextMail("text mail");
				}
			}).start();
			break;

		default:
			break;
		}
	}

}
